package com.digitalchina.lxnsy.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.activity.old.NewAgrH5Activity;
import com.digitalchina.lxnsy.api.BusinessAgr;
import com.digitalchina.lxnsy.base.BaseFragment;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.AgrListVO;
import com.digitalchina.lxnsy.entity.AgrTypeVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.viewholder.AgrListHolder;
import com.digitalchina.lxnsy.viewholder.AgrTypeHolder;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.fragment
 * 创建人：郑皓
 * 创建时间：2019/4/16 15:43
 * 修改备注：
 * ================================================
 */
public class NewAgrFragment extends BaseFragment implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
    @Bind(R.id.recycler_tab)
    EasyRecyclerView recyclerTab;
    @Bind(R.id.recycler_view)
    EasyRecyclerView recyclerView;

    private RecyclerArrayAdapter adapter;
    private RecyclerArrayAdapter adapterTab;
    private int page = 1;
    private int limit = 10;

//    private String firstTypeCode = ""; // 一级目录code
    private String secondTypeId = ""; // 二级类目id
//    private String farmOperationId = ""; // 农事操作id
    private String productStandardId = ""; // 生产标准id
    private List<AgrTypeVO> list;

    private List<AgrListVO> voList = new ArrayList<>();

    @Override
    public View setRootView() {
        return inflate(R.layout.fragment_new_agr);
    }

    @Override
    public void initMember() {

        //顶部tab
        recyclerTab.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
        recyclerTab.setAdapter(adapterTab = new RecyclerArrayAdapter(getActivity()) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new AgrTypeHolder(parent);
            }
        });


        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        recyclerView.addItemDecoration(Utils.setDividerDecoration(getActivity(), 1, 0, 0));

        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(getActivity()) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new AgrListHolder(parent);
            }
        });


        if (getArguments() != null) {
//            firstTypeCode = getArguments().getString("firstTypeCode");
//            farmOperationId = getArguments().getString("farmOperationId");
            productStandardId = getArguments().getString("productStandardId");
            list = (List<AgrTypeVO>) getArguments().getSerializable("list");
            if (list != null && list.size() > 0)
                secondTypeId = list.get(0).getTowLevelId();
            setType();
        }

        onRefresh();
    }

    @Override
    public void addListener() {
        recyclerView.setRefreshListener(this);

        adapter.setMore(R.layout.view_more, this);

        adapter.setNoMore(R.layout.view_nomore);

        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });


        adapterTab.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                for (int i = 0; i < list.size(); i++) {
                    list.get(i).setChecked(i == position);
                }
                adapterTab.notifyDataSetChanged();

                AgrTypeVO vo = (AgrTypeVO) adapterTab.getAllData().get(position);
                secondTypeId = vo.getTowLevelId();
                onRefresh();
            }
        });


        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                AgrListVO vo = (AgrListVO) adapter.getAllData().get(position);
                GotoUtil.gotoActivity(getActivity(), NewAgrH5Activity.class, "AgrListVO", vo);
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                switch (msg.what) {
                    case MSG.MSG_GET_NEW_AGR_LIST_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_NEW_AGR_LIST_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
                return false;
            }
        });
    }

    // 设置顶部tab类型
    private void setType() {
        if (list == null)
            return;

        for (int i = 0; i < list.size(); i++) {
            list.get(i).setChecked(i == 0);
        }

        adapterTab.clear();
        adapterTab.addAll(list);

    }

    @Override
    public void callBack(Object object) {
        voList = FastJsonUtil.getListBean(object.toString(), "newAgricultureList", AgrListVO.class);


        if (voList == null)
            return;

        if (page == 1) { // 刷新
            adapter.clear();
        }

        adapter.addAll(voList);


        if (voList != null) {
            if (voList.size() < limit) {
                adapter.stopMore();
            }
        }
    }

    @Override
    public void rightOnClick() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onRefresh() {
        page = 1;
        BusinessAgr.getNewAgrListV2(getActivity(),  secondTypeId,  productStandardId, page, limit, mHandler);
    }

    @Override
    public void onLoadMore() {
        if (voList != null && voList.size() >= limit) {
            page++;
            BusinessAgr.getNewAgrListV2(getActivity(), secondTypeId,  productStandardId, page, limit, mHandler);
        }
    }
}
